package com.oceansoft.core.jxkh.controller;


import com.oceansoft.core.common.constant.Constant;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.domain.Message;
import com.oceansoft.core.domain.SysDept;
import com.oceansoft.core.jxkh.domain.Department;
import com.oceansoft.core.jxkh.service.IDepartmentService;
import org.omg.CORBA.Request;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * Created by OS on 2017/12/5.
 */

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Resource
    private IDepartmentService departmentService;

    @RequestMapping(value="allDepartmentInfo.json")
    public void allDepartmentInfo(HttpServletRequest request, ModelMap modelMap){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String order = request.getParameter("order");
        HashMap<String, String> paramap = new HashMap<String, String>();
        paramap.put("offset",offset);
        paramap.put("limit",limit);
        paramap.put("order",order);
        Pagination pag = departmentService.findAllDepartmentInfo(paramap);
        modelMap.addAttribute("rows",pag.getList());
        modelMap.addAttribute("total",pag.getTotalRows());
    }

    @RequestMapping(value = "editDepartmentInfo",method = POST)
    public  void editDepartmentInfo(ModelMap modelMap,SysDept sysDept){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            departmentService.editDepartmentInfo(sysDept);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

    @RequestMapping(value="delDepartmentInfo.json")
    public void delDepartmentInfo(int dbid,ModelMap modelMap){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            departmentService.deleteDepartmentInfoByDbid(dbid);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

    @RequestMapping(value="findDepartmentInfo.html")
    public String findDepartmentInfo(int dbid,ModelMap modelMap){
        SysDept sysDept = departmentService.findDepartmentInfoByDbid(dbid);
        modelMap.addAttribute("sysDept",sysDept);
        return "jump/bmgl_bj";
    }

    @RequestMapping(value = "findDepartmentInfoByDeptId.json")
    public void findDepartmentInfoByDeptId(ModelMap modelMap,HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String order = request.getParameter("order");
        String deptId = request.getParameter("deptId");
        HashMap<String,String> map = new HashMap<>();
        map.put("deptId",deptId);
        map.put("offset",offset);
        map.put("limit",limit);
        map.put("order",order);
        Pagination userPag = departmentService.findDepartmentInfoByDeptId(map);
        modelMap.addAttribute("rows",userPag.getList());
        modelMap.addAttribute("total",userPag.getTotalRows());
    }

}
